
package com.lvmama.pangolin.mail;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailSender
{

	public boolean sendTextMail(MailInfo mailInfo) throws MessagingException
	{
		// 判断是否需要身份认证
		MailAuthenticator authenticator = null;
		Properties pro = mailInfo.getProperties();
		if (mailInfo.isValidate())
		{
			// 如果需要身份认证，则创建一个密码验证器
			authenticator = new MailAuthenticator(mailInfo.getUserName(),
					mailInfo.getPassword());
		}
		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
		try
		{
			// 根据session创建一个邮件消息
			Message mailMessage = new MimeMessage(sendMailSession);
			// 创建邮件发送者地址
			Address from = new InternetAddress(mailInfo.getFromAddress());
			// 设置邮件消息的发送者
			mailMessage.setFrom(from);
			// 创建邮件的接收者地址，并设置到邮件消息中
			
			List<String> toList = mailInfo.getToAddressList();
			List<String> ccList = mailInfo.getCcAddressList();
			List<String> bccList = mailInfo.getBccAddressList();
			if(toList != null && !toList.isEmpty())
			{
				Address to = null;
				for (String string : toList)
				{
					to = new InternetAddress(string);
					mailMessage.addRecipient(Message.RecipientType.TO, to);
				}
				
			}
			if(ccList != null && !ccList.isEmpty())
			{
				Address to = null;
				for (String string : ccList)
				{
					to = new InternetAddress(string);
					mailMessage.addRecipient(Message.RecipientType.CC, to);
				}
				
			}
			if(bccList != null && !bccList.isEmpty())
			{
				Address to = null;
				for (String string : bccList)
				{
					to = new InternetAddress(string);
					mailMessage.addRecipient(Message.RecipientType.BCC, to);
				}
				
			}
			// 设置邮件消息的主题
			mailMessage.setSubject(mailInfo.getSubject());
			// 设置邮件消息发送的时间
			mailMessage.setSentDate(new Date());
			// 设置邮件消息的主要内容
			String mailContent = mailInfo.getContent();
			mailMessage.setText(mailContent);
			// 发送邮件
			Transport.send(mailMessage);
			return true;
		}
		catch (MessagingException ex)
		{
			throw new MessagingException("邮件发送异常!",ex);
		}
		
	}

	public static void main(String[] args)
	{
		// 这个类主要是设置邮件
		MailInfo mailInfo = new MailInfo();
		mailInfo.setMailServerHost("smtp.163.com");
		mailInfo.setMailServerPort("25");
		mailInfo.setValidate(true);
		mailInfo.setUserName("ycachi@163.com");
		mailInfo.setPassword("Yc199031348");// 您的邮箱密码
		mailInfo.setFromAddress("ycachi@163.com");
		List<String> toList = new ArrayList<String>();
		List<String> ccList = new ArrayList<String>();
		toList.add("ycachi@126.com");
		ccList.add("yuanchi@lvmama.com");
		mailInfo.setToAddressList(toList);
		mailInfo.setCcAddressList(ccList);
		mailInfo.setSubject("【重要】慧眼系统告警");
		mailInfo.setContent("数据抓取异常，请尽快检查!");
		// 这个类主要来发送邮件
		MailSender sms = new MailSender();
		try
		{
			sms.sendTextMail(mailInfo);
		}
		catch (MessagingException e)
		{
			e.printStackTrace();
		}// 发送文体格式
	}
}
